package com.cj.mobile.iabp.mvp.presenter;

import static com.cj.mobile.iabp.BuildConfig.IS_DEBUG_DATA;

import android.os.Bundle;

import com.cj.mobile.iabp.app.config.CommonRetryWithDelay;
import com.cj.mobile.iabp.mvp.contract.AddRemoveConcernInfoContract;
import com.cj.mobile.iabp.mvp.model.entity.RemoveInfo;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 2025/09/30 18:01
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class AddRemoveConcernInfoPresenter extends BasePresenter<AddRemoveConcernInfoContract.Model, AddRemoveConcernInfoContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;

    // 患者id
    private String id;
    // 患者登记号
    private String code;
    // 患者姓名
    private String name;
    private RemoveInfo info;


    // 拍照图片(本地)
    private ArrayList<String> takePicturesImgs;
    private ArrayList<String> updateImgs;

    @Inject
    public AddRemoveConcernInfoPresenter(AddRemoveConcernInfoContract.Model model, AddRemoveConcernInfoContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.info = null;
        this.takePicturesImgs = null;
        this.updateImgs = null;
    }

    public void getBundleValues(Bundle bundle) {
        if (bundle != null) {
            id = bundle.getString("id");
            code = bundle.getString("code");
            name = bundle.getString("name");
            info = bundle.getParcelable("info");
        }
    }

    public void initData(Bundle bundle) {
        getBundleValues(bundle);

        takePicturesImgs = new ArrayList<>();
        updateImgs = new ArrayList<>();

        // 显示登记号和患者姓名
        mRootView.showRemoveInfo(code, name, info);
    }


    /**
     * 修改撤机信息
     *
     * @param pre       暂存0或提交1
     * @param ast       ast
     * @param alt       alt
     * @param cr        肌酐cr
     * @param egfr      egfr
     * @param myo       肌红蛋白
     * @param clac      乳酸
     * @param ga        肌钙蛋白
     * @param leftSize  左室舒张末期内径
     * @param leftScore 左室射血分数
     * @param nirsLeft  NIRS左
     * @param nirsRight NIRS右
     * @param lessL     安装侧
     * @param lessR     对侧
     */
    public void updateRemove(String pre, String focuse, String ast, String alt, String cr, String egfr, String myo, String clac,
                             String ga, String leftSize, String leftScore, String nirsLeft, String nirsRight, List<String> lessL,
                             List<String> lessR) {
        if (IS_DEBUG_DATA) {
            mRootView.showMessage("修改成功！");
        } else {
            // 修改撤机信息
            mModel.updateRemove(pre, focuse, info.getId(), id, ast, alt, cr, egfr, myo, clac,
                            ga, leftSize, leftScore, nirsLeft, nirsRight, lessL, lessR)
                    .subscribeOn(Schedulers.io())
                    .retryWhen(new CommonRetryWithDelay(2, 2))    // 遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                    .doOnSubscribe(disposable -> {
                        mRootView.showLoadingSubmit();
                    })
                    .subscribeOn(AndroidSchedulers.mainThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doFinally(() -> {
                        mRootView.hideLoadingSubmit();
                    }).compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                    .subscribe(new ErrorHandleSubscriber<Integer>(mErrorHandler) {

                        @Override
                        public void onNext(Integer commonResponse) {
                            mRootView.showMessage("修改成功！");
                            mRootView.killMyself();
                        }
                    });
        }
    }
}